package test;

/**
 * @author Marioszk
 * 圆圈中最后剩下的数字
 */
public class CircleLastRemainNum_圆圈中最后剩下的数字 {

    public static void main(String[] args) {
        System.out.println(lastRemaining2(5, 3));
    }

    public static int lastRemaining(int n, int m) {
        if (n == 1) {
            return 0;
        }
        return (lastRemaining((n - 1), m) + m) % n;
    }

    public static int lastRemaining2(int n, int m) {
        int pos = 0;
        for (int i = 2; i <= n; i++) {
            pos = (pos + m) % i;
        }
        return pos;
    }

}
